package hk.dao.impl;

import hk.dao.DaoGoods;
import hk.jdbc.DBUtil;
import hk.pojo.CateInfo;
import hk.pojo.GoodsHistoryInfo;
import hk.pojo.GoodsInfo;
import hk.pojo.GoodsMenuInfo;
import hk.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/5/17 13:21
 */
public class DaoGoodsImpl implements DaoGoods {
    @Override
    public int addGoods(GoodsInfo goods) {
        String sql = "insert into goodsinfo (goodsName,bigCateId,smallCateId,unit,price,producter,des,pictureData,businessId)  values (?,?,?,?,?,?,?,?,?)";
        Object [] params={
                goods.getGoodsName(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getUnit(),
                goods.getPrice(),
                goods.getProducter(),
                goods.getDes(),
                goods.getPictureData(),
                goods.getBusinessId()
        };
        return DBUtil.addAndReturnId(sql,params);
    }


    @Override
    public int getGoodsCount(int businessId) {
        long goodsCount = DBUtil.getScala("select count(*) from goodsinfo where businessId = ?",businessId);
        return Integer.parseInt(goodsCount+"");
    }

    @Override
    public List<GoodsInfo> getGoodsList(PageInfo page,int businessId) {
        return DBUtil.getList("select * from goodsinfo where businessid = ? limit ?,?",GoodsInfo.class,businessId,page.getBeginRow(),page.getPageSize());
    }

    @Override
    public String getNamaByIdForCate(int bigId) {
        return DBUtil.getScala("select cateName from cateinfo where id = ?",bigId);
    }

    @Override
    public int updateGoods(GoodsInfo goods) {
        String sql = "update goodsinfo set goodsName = ?, bigCateId = ?, smallCateId = ?, price = ?, des = ?, unit = ?, producter = ?, pictureData = ? where id = ?";
        Object[] params = {
                goods.getGoodsName(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getPrice(),
                goods.getDes(),
                goods.getUnit(),
                goods.getProducter(),
                goods.getPictureData(),
                goods.getId()
        };
        return DBUtil.update(sql,params);
    }

    @Override
    public int addHistoryByGoods(GoodsMenuInfo goodsMenuInfo) {
        return DBUtil.update("insert into goodsmenu (goodsName, historyChange, goodsId, historyId) values (?,?,?,?)",goodsMenuInfo.getGoodsName(),goodsMenuInfo.getHistoryChange(),goodsMenuInfo.getGoodsId(),goodsMenuInfo.getHistoryId());
    }

    @Override
    public List<GoodsMenuInfo> getHistoryList(PageInfo page, int goodsId) {
        return DBUtil.getList("select * from goodsmenu where goodsId = ? limit ?,?",GoodsMenuInfo.class,goodsId,page.getBeginRow(),page.getPageSize());

    }

    @Override
    public int getGoodsCountByHis(int goodsId) {
        long goodsCount = DBUtil.getScala("select count(*) from goodsmenu where goodsId = ?",goodsId);
        return Integer.parseInt(goodsCount+"");
    }

    @Override
    public int addBigCate(String bigName, String des) {
        return DBUtil.update("insert into cateinfo (cateName,des,parentId) values (?,?,?)",bigName,des,0);
    }

    @Override
    public List<CateInfo> selectBigName() {
        return DBUtil.getList("select * from cateinfo where parentId = 0",CateInfo.class);
    }

    @Override
    public int addSmallCate(String bigName, String des, int bigCateId) {
        return DBUtil.update("insert into cateinfo (cateName,des,parentId) values (?,?,?)",bigName,des,bigCateId);
    }

    @Override
    public void deleteCheckedIds(String[] checkedIds) {
        StringBuffer sql = new StringBuffer();
        sql.append("delete from cateinfo where id in (");
        for(int i = 0;i<checkedIds.length;i++){
            if(checkedIds.length-1>i){
                sql.append(checkedIds[i]+",");
            }else{
                sql.append(checkedIds[i]+")");
            }
        }
        DBUtil.update(sql+"");
    }

    @Override
    public List<Integer> selectCheckedIds(String[] checkedIds) {
        StringBuffer sql = new StringBuffer();
        sql.append("select id from goodsinfo where smallCateId in (");

        for(int i = 0;i<checkedIds.length;i++){
            if(checkedIds.length-1>i){
                sql.append(checkedIds[i]+",");
            }else{
                sql.append(checkedIds[i]+")");
            }
        }

        return DBUtil.getColumn(sql+"");
    }

    @Override
    public List<String> selectRepeatName(List<Integer> goodsId) {
        if(goodsId.size() > 0){
            StringBuffer sql = new StringBuffer();
            sql.append("select cateName from cateinfo where id in (select smallCateId from goodsinfo where id in ( ");
            for(int i = 0;i<goodsId.size();i++){
                if(i<goodsId.size()-1){
                    sql.append(goodsId.get(i)+",");
                }else{
                    sql.append(goodsId.get(i)+"))");
                }
            }
                return DBUtil.getColumn(sql+"");
        }else {
            return new ArrayList<>();
        }
    }

    @Override
    public List<CateInfo> selectFatherCheckedIds(String[] fatherCheckedId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select * from cateinfo where parentId in ( ");
        for(int i = 0;i<fatherCheckedId.length;i++){
            if(i<fatherCheckedId.length-1){
                sql.append(fatherCheckedId[i]+",");
            }else{
                sql.append(fatherCheckedId[i]+")");
            }
        }
        return DBUtil.getList(sql+"",CateInfo.class);
    }

    @Override
    public int saveHistory(GoodsInfo goods) {
        String sql = "insert into goodshistory (goodsName,bigCateId,smallCateId,unit,price,producter,des,pictureData,businessId)  values (?,?,?,?,?,?,?,?,?)";
        Object [] params={
                goods.getGoodsName(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getUnit(),
                goods.getPrice(),
                goods.getProducter(),
                goods.getDes(),
                goods.getPictureData(),
                goods.getBusinessId()
        };
        return DBUtil.addAndReturnId(sql,params);
    }

    @Override
    public GoodsHistoryInfo getGoodsByIdInHis(int histroyId) {
        return DBUtil.getSingleObj("select * from goodshistory where id = ?",GoodsHistoryInfo.class,histroyId);
    }
    @Override
    public GoodsInfo getGoodsById(int goodsId) {
        return DBUtil.getSingleObj("select * from goodsinfo where id = ?",GoodsInfo.class,goodsId);
    }


}
